<template>
  <div ref="ac">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>协同办公</el-breadcrumb-item>
      <el-breadcrumb-item>流程模型</el-breadcrumb-item>
    </el-breadcrumb>
    <div v-if="!showIframe" class="app-container">
      <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
        <el-form-item label="KEY" prop="key">
          <el-input
            v-model="queryParams.key"
            placeholder="请输入KEY"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入名称"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >创建新模型</el-button>
        </el-col>
<!--  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
      </el-row>
  <!-- 表格展示模型列表  v-loading="loading"   -->
      <el-table   :data="modelerList">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="KEY" align="center" prop="key" width="120" />
        <el-table-column label="名称" align="center" prop="name" width="150" />
        <el-table-column label="版本" align="center" prop="version" width="150" />
        <el-table-column label="创建时间" align="center" width="180">
          <template slot-scope="scope">
          {{scope.row.createTime | dateFormat}}
        </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" width="150" />
        <el-table-column label="最后更新时间" align="center" width="180">
          <template slot-scope="scope">
            {{scope.row.lastUpdateTime | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"

            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-upload2"
              @click="handleDeploy(scope.row)"
            >部署</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-download"
              @click="handleExport(scope.row)"
            >导出</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
<!--      <pagination-->
<!--        v-show="total>0"-->
<!--        :total="total"-->
<!--        :page.sync="queryParams.pageNum"-->
<!--        :limit.sync="queryParams.pageSize"-->
<!--        @pagination="getList"-->
<!--      />-->
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="float: right">
      </el-pagination>

      <!-- 添加或修改模型对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="KEY" prop="key">
            <el-input v-model="form.key" placeholder="请输入KEY" />
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入名称" />
          </el-form-item>
          <el-form-item label="描述" prop="description">
            <el-input v-model="form.description" type="textarea" placeholder="请输入描述" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>


    </div>
  <!--vue使用iframe嵌套在线流程设计器-->
    <div v-if="showIframe">
          <span style="font-weight: bolder">在线流程设计</span>
          <span style="width:50px;background-color: #0086b3;float: right;text-align: center;color: #F3F1DF;" @click="closeIframe">X</span>
      <iframe style="width: 100%;height: 800px" :src="url" frameborder="0" scrolling="auto"></iframe>
    </div>

  </div>

</template>

<script>
 import {resetForm} from "@/plugins/ruoyi.js"

  export default {
    name: 'MyProcess',
    //数据
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 模型表格数据
        modelerList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          key: '',
          name: '',
        },
        // 表单参数
        form: {},
        defaultProps: {
          children: "children",
          label: "label"
        },
        url: '',
        showIframe: false,
        // 表单校验
        rules: {
          key: [
            { required: true, message: "KEY不能为空", trigger: "blur" }
          ],
          name: [
            { required: true, message: "名称不能为空", trigger: "blur" }
          ],
        }
      };

    },
    /*页面初始化加载*/
    created(){
      //加载模型列表
      this.getList();
    },
    //方法
    methods: {
      //关闭设计器
      closeIframe(){
        this.showIframe=false;
      },
      // 重置表单
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          key: undefined,
          description: undefined,
        };
        this.resetForm("form");
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      //重置
      resetQuery(){
        this.$refs.queryForm.resetFields()
      },
      //查询
      handleQuery(){
        this.queryParams.pageNum=1;
        this.queryParams.pageSize=10;
        this.getList();
      },
      //表单提交
      submitForm(){
        this.$refs["form"].validate(valid=>{
          if(valid){
            this.$http.post("/activiti/modeler/create",this.form).then(res=>{
              console.log(res.data.code)
              if(res.data.code===200){
                this.designModel(res.data.data)
              }else{
                this.message.error(res.data.msg);
              }
          }).catch(err =>{
              console.log(err)
            });
          }
        });
      },
      //模型在线设计
      designModel(id){
        // 这里配置的是后端的请求地址，后期改成服务器地址，
        this.open = false;
        this.url = 'http://127.0.0.1:8090/modeler.html?modelId=' + id;
        this.showIframe = true;

      },
      /*分页*/
      handleCurrentChange(pageNum){
        this.queryParams.pageNum=pageNum;
        this.getList();
      },
      /*获取模型列表*/
      getList(){
       // this.loading=true;
        this.$http.get("/activiti/modeler/list",{params:this.queryParams}).then(res=>{
          console.log(res)
         if(res.data.code==200){
           this.modelerList=res.data.rows;
           this.total = res.data.total;
          // this.loading = false;
         }else{
           alert("列表查询出错");
         }
        }).catch(err=>{
          alert("程序出错")
        })
      },
      /* 添加新模型*/
      handleAdd(data) {
        this.reset();
        this.open = true;
        this.title = "添加模型";
        /*
        /activiti/modeler/create
        **/
      },
      /*修改模型*/
      handleUpdate(row){
        this.reset();
        const  modeId=row.id;
        this.designModel(modeId);
      },
      /*
      /activiti/modeler/export/' + modelId, get
      * */
      handleExport(row){

      },
      /*部署模型
      /activiti/modeler/deploy/' + modelId, get
      */
      handleDeploy(row){},
      /*删除模型*/
      handleDelete(row){
        const modelIds = row.id;
        this.$confirm('是否确认删除ID为"' + row.name + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=>{
          this.$http.get("/activiti/modeler/remove/"+modelIds).then(rest=>{
            if(rest.data.code==200){
             this.$message(rest.data.msg);
             this.getList();
            }
          })
        }).catch(function () {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
    }
  }

</script>

<style scoped>

</style>
